// userRecharge.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: 2,
    grid:[
      {
        id:1,
        price:10
      }, {
        id: 2,
        price: 20
      }, {
        id: 3,
        price: 50
      }, {
        id: 4,
        price: 100
      }, {
        id: 5,
        price: 200
      }, {
        id: 6,
        price: null,
        type:'input'
      },
    ],
    amount:'',
    price:''
  },
  onChange(event) {
    var list = this.data.grid;
    var price = this.data.price
    this.setData({
      radio: event.detail
    });
    if(event.detail>=0 && event.detail<5){
      this.setData({
        amount: list[event.detail].price
      });
    }
  },
  priceChange:function(event){
    this.setData({
      amount: parseInt(event.detail),
      price: parseInt(event.detail)
    })
  },
  submit:function(){
    var that = this;
    var item = parseInt(that.data.radio);
    var amount = that.data.amount;
    var id = ''
    if (item >= 0 && item < 5) {
      amount = that.data.grid[item].price
    }
    if(amount){
      var data = { amount: amount}
      app.post('order/reCharge',data).then((res)=>{
        if(res.code==200){
          console.log(res)
          id = res.entity.id;
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            signType: res.data.signType,
            package: res.data.package,
            paySign: res.data.paySign,
            success(res) {
              console.log('成功', res)
              wx.showToast({
                title: '充值成功',
                icon: 'success',
                duration: 2000
              })
              that.reChargeCallBack(id,'success');
              var pages = getCurrentPages();
              var beforePage = pages[pages.length - 2];
              beforePage.onLoad();
              wx.navigateBack({
                delta: 1
              })
            },
            fail(res) {
              console.log('失败', res)
              wx.showToast({
                title: '充值失败',
                icon: 'success',
                duration: 2000
              })
              that.reChargeCallBack(id,'fail');
            }
          })
        }
      }).catch((errMsg)=>{
        console.log(errMsg)
      })
      console.info("amount",amount)
    }else{
      Toast.loading('请填写自定义金额')
    }
  },
  reChargeCallBack:function(id,status){
    var data = {id:id,status:status}
    app.post('order/reChargeCallBack', data).then((res) => {
      console.log(res)
    }).catch((errMsg) => {
      console.log(errMsg)
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  }
})